// 验证表
var forms = document.getElementsByTagName("form")[0];
// console.log(forms);
var inps = document.getElementsByTagName("input");
// console.log(inps);
for (var i = 0; i < inps.length - 2; i++) {
    inps[i].onfocus = function () {
        if (this.name == "in1") {
            this.nextElementSibling.innerHTML = "请输入昵称"
        } else if (this.name == "in2") {
            this.nextElementSibling.innerHTML = `<p>8-16位大小写字母和数字组合</P>`;
        }
        else if (this.name == "in3") {
            this.nextElementSibling.innerHTML = "输入手机号"
        } else if (this.name == "in4") {
            this.nextElementSibling.innerHTML = "请输入正确的验证码"
        }
        this.nextElementSibling.style.color = "#fff"
    }
    //验证
    inps[i].onblur = function () {
        var user = this.value;
        if (this.name == "in1") {
            var reg = /^.{2,10}$/;
        } else if (this.name == "in2") {
            var reg = /^(?![0-9A-Z]+$)(?![0-9a-z]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{4,8}$/;
        } else if (this.name = "in3") {
            var reg = /^(\+86|0086)?\s*1[3-9]\d{9}$/;
        } else if (this.name == "in4") {
        if (this.value == inps[4].value) {
                var reg = /^[0-9a-z]{4}$/
            } else {
                var reg = /^(?![0-9]+$)\d$/
            }
        }
        var bool = reg.test(user);
        if (bool) {
            this.nextElementSibling.innerHTML = "格式正确";
            this.nextElementSibling.style.color = "green"
        } else {
            this.nextElementSibling.innerHTML = "格式不正确";
            this.nextElementSibling.style.color = "red"
        }
        if (user == "") {
            this.nextElementSibling.innerHTML = "不能为空";
            this.nextElementSibling.style.color = "red";
        }
    }
}

forms.onsubmit = function () {
    inps[0].onblur();
    inps[1].onblur();
    inps[2].onblur();
    inps[3].onblur();
    for (var i = 0; i < inps.length; i++) {
        if (inps[i].nextElementSibling.style.color == "red") {
            return false;
        }
    }
}
// 随机验证码
var arr = [];
f1(48, 52);
// f1(65, 90);
f1(97, 122);
inps[4].onclick = function () {
    var codes = "";
    while (codes.length < 4) {
        var r = parseInt(Math.random() * (arr.length));
        if (codes.indexOf(arr[r]) == -1) {
            codes += arr[r];
        }
    }
    this.value = codes;
}


function f1(a, b) {
    for (var i = a; i <= b; i++) {
        arr.push(String.fromCharCode(i))
    }
}


// 验证表
var inps = form.getElementsByTagName("input");

